home *** CD-ROM | disk | FTP | other *** search
/ Greenhouse Effect Detection Expriment / NASA Greenhouse Effect Detection Expriment 1992 - Disc 2.iso / software / dos / cdf22pc / src / tools / coltorow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-19  |  3.6 KB  |  109 lines

  1. /******************************************************************************
  2. *
  3. *  NSSDC/CDF                  Convert from COLUMN major to ROW major.
  4. *
  5. *  Version 1.2, 20-Feb-92, ST Systems (STX)
  6. *
  7. *  Modification history:
  8. *
  9. *   V1.0  15-May-91, J Love    Original version.
  10. *   V1.1  29-Oct-91, J Love    Added needed system include files.
  11. *   V1.2  20-Feb-92, J Love    Changed to have an input and output buffer
  12. *                rather than switching the majority in place.
  13. *
  14. ******************************************************************************/
  15.  
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19.  
  20. #define MAX_DIMS    10            /* same as CDF_MAX_DIMS */
  21.  
  22. #if defined(unix)
  23. #define memmove(a,b,x) bcopy(b,a,x)
  24. #endif
  25.  
  26. /******************************************************************************
  27. * Macro to calculate the indices for a given value offset (column major).
  28. ******************************************************************************/
  29.  
  30. #define COLindices(valueOffset,Ndims,products,indices) { \
  31. long valueOffsetT = valueOffset; \
  32. long i; \
  33. for (i = Ndims - 1; i >= 0; i--) { \
  34.    indices[i] = valueOffsetT / products[i]; \
  35.    valueOffsetT = valueOffsetT % products[i]; \
  36. } \
  37. }
  38.  
  39. /******************************************************************************
  40. * Macro to calculate the value offset for a given set of indices (row major).
  41. ******************************************************************************/
  42.  
  43. #define ROWoffset(indices,Ndims,products,valueOffset) { \
  44. long i; \
  45. valueOffset = 0; \
  46. for (i = Ndims - 1; i >= 0; i--) valueOffset += indices[i] * products[i]; \
  47. }
  48.  
  49. /******************************************************************************
  50. * COLtoROW.
  51. ******************************************************************************/
  52.  
  53. void COLtoROW (iBuffer, oBuffer, Ndims, dimSizes, NvalueBytes)
  54. void *iBuffer;
  55. void *oBuffer;
  56. long Ndims;
  57. long dimSizes[];
  58. long NvalueBytes;    /* size (in bytes) of each value */
  59. {
  60. long indices[MAX_DIMS];
  61. long ROWproducts[MAX_DIMS];
  62. long COLproducts[MAX_DIMS];
  63. long Nvalues;
  64. long i, j;
  65.  
  66. /******************************************************************************
  67. * Calculate the number of values in the array.
  68. ******************************************************************************/
  69.  
  70. Nvalues = 1;
  71. for (i = 0; i < Ndims; i++) Nvalues *= dimSizes[i];
  72.  
  73. /******************************************************************************
  74. * Don't do anything except copy if less than 2 dimensions.  Row and column
  75. * major are the same for 0 or 1 dimensions.
  76. ******************************************************************************/
  77.  
  78. if (Ndims < 2) {
  79.   memmove (oBuffer, iBuffer, (size_t) (Nvalues * NvalueBytes));
  80.   return;
  81. }
  82.  
  83. /******************************************************************************
  84. * Calculate the row and column products (what each dimension is worth).
  85. ******************************************************************************/
  86.  
  87. for (i = 0; i < Ndims; i++) {
  88.    ROWproducts[i] = 1;
  89.    for (j = i + 1; j < Ndims; j++) ROWproducts[i] *= dimSizes[j];
  90.    COLproducts[i] = 1;
  91.    for (j = 0; j < i; j++) COLproducts[i] *= dimSizes[j];
  92. }
  93.  
  94. /******************************************************************************
  95. * For each value in the column major array, calculate its indices and then
  96. * calculate the corresponding value offset in the row major array (then move
  97. * it into the row major array).
  98. ******************************************************************************/
  99.  
  100. for (i = 0; i < Nvalues; i++) {
  101.    COLindices (i, Ndims, COLproducts, indices);
  102.    ROWoffset (indices, Ndims, ROWproducts, j);
  103.    memmove ((char *) oBuffer + (j * NvalueBytes),
  104.         (char *) iBuffer + (i * NvalueBytes), (size_t) NvalueBytes);
  105. }
  106.  
  107. return;
  108. }
  109.